
/**
 *	\file
 *	Definition of the nmn::detail::assert_message function.
 *	\author Sean Tromans
 */

#ifndef	NMN_ASSERT_MESSAGE_HPP
#define	NMN_ASSERT_MESSAGE_HPP

#define	NMN_MAX_ASSERT_LINE_LENGTH	(60)

#if	defined(WIN32)
#include <Windows.h>
#include <signal.h>
#endif

namespace nmn
{
	namespace detail
	{

		/**
		 *	Function to process the expression being asserted.
		 *	@param expression The boolean expression being asserted.
		 *	@param message The message to output upon failure.
		 *	@param filename The file which triggered the assertion.
		 *	@param line The line which triggered the assertion.
		 *	@return true if the program should trigger a breakpoint.
		 *	\author Sean Tromans
		 */
		inline bool	assert_message( bool expression, std::string const & message, std::string const & filename, size_t line )
		{
			if ( expression )
			{
				return false;
			}

			std::string output( "Assertion Triggered in file:\n" );
			std::string file( filename );

			if ( file.size( ) > NMN_MAX_ASSERT_LINE_LENGTH )
			{
				file = file.substr( file.size( ) - NMN_MAX_ASSERT_LINE_LENGTH, NMN_MAX_ASSERT_LINE_LENGTH );
				file.insert( 0, "..." );
			}

			output.append( file );
			output.append( nmn::create_string( "\nOn Line: %1%\n\n", line ) );
			output.append( message );

			nmn::output_message( output );

#if	defined(WIN32)

			int result = MessageBoxA( NULL, output.c_str( ), "Assert Error", MB_ABORTRETRYIGNORE | MB_ICONWARNING );

			if ( result == IDRETRY )
			{
				return true;
			}
			else if ( result == IDABORT )
			{
				raise(SIGABRT);
				exit(1);
			}

#else
			exit(1);
#endif

			return false;
		}
	};
};

#endif	//NMN_ASSERT_MESSAGE_HPP
